package com.qwf.shici.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.qwf.shici.R;

import java.util.List;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    protected Toolbar toolbar;
    protected TextView tv_title;//标题
    protected TextView tv_right_text;//标题栏右上角的文字

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        toolbar = findViewById(R.id.toolbar);
        tv_title = findViewById(R.id.tv_title);
        tv_right_text = findViewById(R.id.tv_right_text);

        if (null != tv_right_text) {
            tv_right_text.setOnClickListener(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setToolBar();
    }

    /**
     * 初始化toolBar
     */
    protected void setToolBar() {
        if (null != toolbar) {
            toolbar.postDelayed(new Runnable() {
                @Override
                public void run() {
                    toolbar.setNavigationIcon(R.drawable.arad_ic_back_selector);
                    toolbar.setNavigationOnClickListener(BaseActivity.this);
                    toolbar.setTitle("");
                }
            }, 1);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == -1) {//点击的是toolbar左上角的按钮
            onBackPressed();
        }
        if (v == tv_right_text) {//点击右上角的标题
            onRightTextClick();
        }
    }

    /**
     * 点击右上角文字按钮触发该方法
     */
    protected void onRightTextClick() {
    }

    /**
     * 设置标题栏的标题
     *
     * @param title
     */
    protected void setHeadTitle(String title) {
        if (null != tv_title) {
            tv_title.setText(title);
        }
    }

    /**
     * 设置标题栏右上角的文字
     *
     * @param rightText
     */
    protected void setRightText(String rightText) {
        if (null != tv_right_text) {
            tv_right_text.setText(rightText);
        }
    }

    /**
     * 设置标题栏返回按钮图标
     *
     * @param iconId
     */
    protected void setNavigationIcon(int iconId) {
        if (null != toolbar) {
            toolbar.setNavigationIcon(iconId);
        }
    }

    /**
     * 设置标题栏返回按钮图标
     */
    protected void setNoNavigationIcon() {
        if (null != toolbar) {
            toolbar.setNavigationIcon(null);
        }
    }

    /**
     * 判断列表数据是否为空
     *
     * @param list
     * @return
     */
    public boolean isDataEmpty(List list) {//判断list数据是否为空:null 或者size = 0 视为空
        return null == list || list.size() == 0;
    }

    /**
     * 回收bitmap
     *
     * @param bitmap
     */
    protected void recycleBitmap(Bitmap bitmap) {
        if (null != bitmap && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }

    /**
     * 加载布局文件
     *
     * @return
     */
    protected abstract int getLayoutRes();
}
